package lyl.stack;

import java.util.Stack;

public class MinStack {
    Stack<Integer> stack;
    Stack<Integer> minStack;
    public MinStack(){
        stack =new Stack<>();
        minStack = new Stack<>();
    }
    public void push(int number){
        if(minStack.isEmpty()){
            stack.push(number);
            minStack.push(number);
        }else {
            if(number < minStack.peek()){
                minStack.push(number);
            }else {
                minStack.push(minStack.peek());
            }
            stack.push(number);
        }
    }

    public int pop() throws Exception{
        if(!stack.isEmpty()){
            minStack.pop();
            return stack.pop();
        }
        throw new Exception();
    }

    public int min() throws Exception{
        if(!minStack.isEmpty()){
            return minStack.peek();
        }
        throw new Exception();
    }
}
